<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
    <meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0 minimal-ui"></meta>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
    <meta name="apple-mobile-web-app-capable" content="yes"></meta>
    <meta name="apple-mobile-web-app-status-bar-style" content="white"></meta>
    <meta http-equiv="Access-Control-Allow-Origin" content="*">

    <title>大转盘抽奖啦</title>
    <link href="css/style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="js/jquery-3.2.1.js"></script>
    <script type="text/javascript" src="js/jQueryRotate.js"></script>
    <style type="text/css">
        #lw_loading_bg {
            position: fixed;
            display: none;
            z-index: 2000;
            top: 0px;
            left: 0px;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.150)
        }

        #lw_popup_bg {
            font-size: 14px;
            font-family: '微软雅黑';
            color: #000;
            padding: 10px;
            position: absolute;
            display: none;
            z-index: 3000;
            background-color: #FFF;
            left: 50%;
            top: 55%;
            height: auto;
            width: 250px;
            margin-left: -130px;
            text-align: center;
            border: 0px solid #03F;
            border-radius: 8px;
        }

        #lw_popup_ok {
            margin: 0 auto;
            background-color: #36F;
            width: 90%;
            line-height: 25px;
            text-align: center;
            margin-top: 15px;
        }

        #lw_popup_ok a {
            text-decoration: none;
            font-family: "微软雅黑";
            font-size: 14px;
            color: #FFF;
            display: block;
            width: 100%;
            height: 100%;
        }
    </style>
    <script type="text/javascript">
        var turnplate = {
            restaraunts: [],				//大转盘奖品名称
            colors: [],					//大转盘奖品区块对应背景颜色
            outsideRadius: 192,			//大转盘外圆的半径
            textRadius: 155,				//大转盘奖品位置距离圆心的距离
            insideRadius: 68,			//大转盘内圆的半径
            startAngle: 0,				//开始角度
            times: 3,

        };
        //动态添加大转盘的奖品与奖品区域背景颜色
        turnplate.restaraunts = ["5元话费", "100元话费", "15元话费", "50元话费", "谢谢参与", "30元话费", "20元话费", "70元话费 ", "10元话费", "谢谢参与"];
        turnplate.colors = ["#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF"];

        $(document).ready(function () {
            setInterval('autoScroll("#lwNoticeInfo")', 4000)
            var rotateTimeOut = function () {
                $('#wheelcanvas').rotate({
                    angle: 0,
                    animateTo: 2160,
                    duration: 3000,
                    callback: function () {
                        alert('网络超时，请检查您的网络设置！');
                    }
                });
            };


            $.ajax(
                {
                    type: 'get',
                    url: 'http://www.yanglun.win:8081/getBannerTips',
                    dataType: 'jsonp',
                    jsonp: "callback",
                    jsonpCallback: "success_jsonpCallback",
                    success: function (json) {
                        $.each(json.body, function (i, n) {
                            console.log("中奖用户:" + n.user + " 商品:" + n.gift);
                            $('#lwNoticeInfo ul').append("<li>恭喜" + n.user + "用户，抽中<font color=\"red\">" + n.gift + "</font>一台</li>");
                        });
                    },
                    error: function (xhr, status, error) {
                        alert('fail' + error);
                    }
                }
            );

            var bRotate = false;				//false:停止;ture:旋转
            var bPointerClickable = true;
            var timer = 0;
            var itemIndex = 0;
            $('.pointer').click(function () {
                if (bPointerClickable == false || turnplate.times == 0) {
                    return;
                } else {
                    $.ajax(
                        {
                            type: 'get',
                            url: 'http://www.yanglun.win:8081/getGiftRandomNo',
                            dataType: 'jsonp',
                            jsonp: "callback",
                            jsonpCallback: "success_jsonpCallback",
                            success: function (json) {
                                itemIndex = json.giftNum;
                                bRotate = false;
                                bPointerClickable = false;
                                console.log("服务端结果返回，触发递归结束----" + json.giftNum);
                                turnplate.times--;
                                $('.lwDescribe #lw_times').text(turnplate.times)

                            },
                            error: function (xhr, status, error) {
                                alert('fail' + error);
                            }
                        }
                    );
                }

                var angle = 0;
                if (bRotate == false) {
                    bRotate = true;
                    var rotation = function () {

                        var rotation2 = function () {
                            if (bRotate == true) {
                                console.log("递归----")
                                $('#wheelcanvas').stopRotate();
                                $('#wheelcanvas').rotate({
                                    angle: 0,
                                    animateTo: 720,
                                    duration: 2000,
                                    callback: rotation2,
                                    easing: function (x, t, b, c, d) {
                                        return c * (t / d) + b;
                                        return -c * ((t = t / d - 1) * t * t * t - 1) + b;
                                    }
                                });
                            } else {
                                console.log("递归结束开始执行----")
                                var item = itemIndex;
                                var angles = item * (360 / turnplate.restaraunts.length) - (360 / (turnplate.restaraunts.length * 2));
                                if (angles < 270) {
                                    angles = 270 - angles;
                                } else {
                                    angles = 360 - angles + 270;
                                }
                                var dur = 0;
                                //商品角度大于180度，多增加1秒钟
                                if (angles > 180) {
                                    dur = 1000
                                }
                                $('#wheelcanvas').rotate({
                                    angle: 0,
                                    animateTo: 360 + angles,
                                    duration: 4000 + dur,
                                    callback: function () {
                                        console.log("递归结束执行完毕----")
                                        bPointerClickable = true;
                                        $('#lw_gift_text').text('恭喜您，获得' + turnplate.restaraunts[item - 1]);
                                        $("#lw_loading_bg").css("display", 'block');
                                        $('#lw_popup_bg').show();
                                    },
                                    easing: function (x, t, b, c, d) {
                                        return -c * ((t = t / d - 1) * t * t * t - 1) + b;
                                    }
                                });
                            }
                        }
                        rotation2();
                    }
                    rotation();
                }
//                else {
//                    bRotate = false;
//                    bPointerClickable = false;
//                    console.log("触发递归结束----")
//                }
            });
        });

        function autoScroll(obj) {
            $(obj).find("ul:first").animate({
                    marginTop: "-22px"
                },
                500,
                function () {
                    $(this).css({
                        marginTop: "0px"
                    }).find("li:first").appendTo(this);
                });
        }

        function rnd(n, m) {
            var random = Math.floor(Math.random() * (m - n + 1) + n);
            return random;

        }

        function drawRouletteWheel() {
            var canvas = document.getElementById("wheelcanvas");
            if (canvas.getContext) {
                //根据奖品个数计算圆周角度
                var arc = Math.PI / (turnplate.restaraunts.length / 2);
                var ctx = canvas.getContext("2d");
                //在给定矩形内清空一个矩形
                ctx.clearRect(0, 0, 422, 422);
                //strokeStyle 属性设置或返回用于笔触的颜色、渐变或模式
                ctx.strokeStyle = "#FFBE04";
                //font 属性设置或返回画布上文本内容的当前字体属性
                ctx.font = '16px Microsoft YaHei';
                for (var i = 0; i < turnplate.restaraunts.length; i++) {
                    var angle = turnplate.startAngle + i * arc;
                    ctx.fillStyle = turnplate.colors[i];
                    ctx.beginPath();
                    //arc(x,y,r,起始角,结束角,绘制方向) 方法创建弧/曲线（用于创建圆或部分圆）
                    ctx.arc(211, 211, turnplate.outsideRadius, angle, angle + arc, false);
                    ctx.arc(211, 211, turnplate.insideRadius, angle + arc, angle, true);
                    ctx.stroke();
                    ctx.fill();
                    //锁画布(为了保存之前的画布状态)
                    ctx.save();

                    //----绘制奖品开始----
                    ctx.fillStyle = "#E5302F";
                    var text = turnplate.restaraunts[i];
                    var line_height = 20;
                    //translate方法重新映射画布上的 (0,0) 位置
                    ctx.translate(211 + Math.cos(angle + arc / 2) * turnplate.textRadius, 211 + Math.sin(angle + arc / 2) * turnplate.textRadius);

                    //rotate方法旋转当前的绘图
                    ctx.rotate(angle + arc / 2 + Math.PI / 2);

                    /** 下面代码根据奖品类型、奖品名称长度渲染不同效果，如字体、颜色、图片效果。(具体根据实际情况改变) **/
                    if (text.indexOf("元") > 0) {//流量包
                        var texts = text.split("元");
                        for (var j = 0; j < texts.length; j++) {
//                            ctx.font = j == 0 ? 'bold 20px Microsoft YaHei' : '16px Microsoft YaHei';
                            if (j == 0) {
                                ctx.fillText(texts[j] + "元", -ctx.measureText(texts[j] + "元").width / 2, (j * line_height) + 15);
                            } else {
                                ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, (j * line_height) + 15);
                            }
                        }
                    } else if (text.indexOf("M") == -1 && text.length > 6) {//奖品名称长度超过一定范围
                        text = text.substring(0, 6) + "||" + text.substring(6);
                        var texts = text.split("||");
                        for (var j = 0; j < texts.length; j++) {
                            ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
                        }
                    } else {
                        //在画布上绘制填色的文本。文本的默认颜色是黑色
                        //measureText()方法返回包含一个对象，该对象包含以像素计的指定字体宽度
                        ctx.fillText(text, -ctx.measureText(text).width / 2, 15);
                    }

                    //添加对应图标
                    if (text.indexOf("闪币") > 0) {
//                        var img = document.getElementById("shan-img");
//                        img.onload = function () {
//                            ctx.drawImage(img, -15, 10);
//                        };
//                        ctx.drawImage(img, -15, 10);
                    } else if (text.indexOf("谢谢参与") >= 0) {
//                        var img = document.getElementById("sorry-img");
//                        img.onload = function () {
//                            ctx.drawImage(img, -15, 25);
//                        };
//                        ctx.drawImage(img, -15, 25);
                    }
                    //把当前画布返回（调整）到上一个save()状态之前
                    ctx.restore();
                    //----绘制奖品结束----
                }
            }
            //页面所有元素加载完毕后执行drawRouletteWheel()方法对转盘进行渲染

            $("#lw_popup_ok a").click(function () {
                $('#lw_popup_bg').hide();
                $("#lw_loading_bg").css("display", 'none');
            });
        }

        window.onload = function () {
            drawRouletteWheel();
        };
    </script>

</head>
<body class="lwBody">
<div style="margin:0 auto">
    <img class="lwTitle" src="images/lw_title.png"/>
    <div class="lwDescribe">
        在2017.7.20至2017.7.31期间用户累计交易量每满2万元可获得一次抽奖机会<br>
        您已获得 3 次抽奖机会，剩余 <font color="red" id="lw_times">3</font> 次机会抽奖<br>
        <!--<marquee behavior="scroll">您已抽中<font color="red">5元话费 </font></marquee>-->
        <div id="lwNoticeInfo" style="overflow:hidden;height:22px;line-height:22px;">
            <ul>
                <!--<li>恭喜137****9040用户，抽中<font color="red">iPhone7 plus</font>一台</li>-->
                <!--<li>恭喜137****9040用户，抽中<font color="red">苹果笔记本</font>一台</li>-->
                <!--<li>恭喜137****9040用户，抽中<font color="red">飞利浦空气进化器</font>一台</li>-->
                <!--<li>恭喜137****9040用户，抽中<font color="red">小米空气净化器2</font>一台</li>-->
            </ul>
        </div>
    </div>
    <div class="lwWheel_bg">
        <div class="turnplate" style="background-image:url(images/turnplate-bg.png);background-size:100% 100%;">
            <canvas class="item" id="wheelcanvas" width="422px" height="422px"></canvas>
            <img class="pointer" src="images/turnplate-pointer.png"/>
        </div>
    </div>
    <div class="lwRecorder">
        <a href="./GiftRecordList.html" target="_self"> <u> 我的中奖记录</u></a>
    </div>
    <div class="lwDescribe_foot">
        活动结束后由公司客服人员与用户确认并完成奖品发放，若有疑问请联系我司客服
    </div>
</div>
<div id="lw_loading_bg"></div>
<div id="lw_popup_bg">
    <span id="lw_gift_text">恭喜您，抽中50元话费</span>
    <div id="lw_popup_ok">
        <a href="javascript:void(0)">OK</a>
    </div>
</div>
</body>
</html>